//
//  PersonageViewController.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/2/25.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "PersonageViewController.h"


#import "ZiViewController.h"//个人资料
#import "MyLetterViewController.h"//我的站内信
#import "ChengJiViewController.h"//我的业绩
#import "MyMoneyViewController.h"//我的佣金
#import "AboutViewController.h"
#import "ViewController.h"

#import "TodoHttpManager.h"
#import "UIButton+WebCache.h"

#import "PeopleOneCell.h"
#import "PeopleTwoCell.h"
@interface PersonageViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSString *uid;
    NSString *_headerImg;
    NSString *_nameStr;
    
}
@property(nonatomic,strong)UILabel *userName;
@property(nonatomic,strong)NSMutableDictionary *peopleDic;
@property(nonatomic,strong)UITableView *tabView;

@property(nonatomic,strong)NSArray *imgArr;
@property(nonatomic,strong)NSArray *titleArr;
@end

@implementation PersonageViewController

-(void)viewWillAppear:(BOOL)animated
{

    NSUserDefaults *userDefa = [NSUserDefaults standardUserDefaults];
    
   uid= [userDefa objectForKey:@"USER_ID"];
    //个人信息获得
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDGetMineGetInfo:) name:TDGetMineGetInfo object:nil];
    NSLog(@"------这里边有什么-----%@",uid);
    //个人信息获得   在这吧网络请求换了  你不是会新的网络请求了....suanle
    
    [_manager userGetMineInfoGetWithID:uid];//你先别动,我给你演示一下   看见没? 点进去啊 你能不能别动

}



-(void)didTDGetMineGetInfo:(NSNotification *)tific
{
    //biedong 没有走这个啊en 我也不知道怎么回事  这个是你网络请求成功失败都走的不是只要发请求就走这个  那估计是请求没法出去  这个网络提示是怎么出来的  在那个地方写的我也不知道拿出来的
    NSLog(@"-----%@",tific.object);
    
    if(tific.object != nil)
    {
        //这个如果有数据就是成功了吗
        _peopleDic = [tific.object objectForKey:@"data"];
       NSLog( @"里边的跟人信息--peopleDic-0--%@",_peopleDic);

        _nameStr = [_peopleDic objectForKey:@"name"];
        _headerImg = [_peopleDic objectForKey:@"img"];
        
        [_tabView reloadData];
        
    

        
    }
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:TDGetMineGetInfo object:nil];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人中心";
    self.view.backgroundColor = BgColor;
//    NSString *url = [NSString stringWithFormat:@""];
//    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
//    [dict setObject:uid forKey:@"id"];
//    //点进去 复制出来
//    [XX_HttpAFNetManager requestWithType:HttpRequestTypePost withUrlString:url withParaments:dict withSuccessBlock:^(NSDictionary *object) {
//        //成功后回调object是个返回的字典
//        NSLog(@"");
//
//    } withFailureBlock:^(NSError *error) {
//        //失败回调 error是错误信息
//
//    } progress:^(float progress) {
//        //当前请求的进度 这个一般用不到
//
//    }];
//
    _peopleDic = [NSMutableDictionary dictionary];
    _imgArr = @[@"Me_header",@"znx",@"yeji"];
    _titleArr = @[@"",@" 我的站内信",@"我的业绩"];
    _manager = [TodoHttpManager sharedManager];
    _tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, B_HEIGHT) style:UITableViewStyleGrouped];
    _tabView.delegate = self;
    _tabView.dataSource = self;
    
    _tabView.backgroundColor = BgColor;
    [_tabView registerClass:[PeopleOneCell class] forCellReuseIdentifier:@"CellE"];
    [_tabView registerClass:[PeopleTwoCell class] forCellReuseIdentifier:@"CellA"];

    [self.view addSubview:_tabView];
    
    
  
}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _imgArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
       NSString *IDCell = @"Cell";
        PeopleOneCell *cell = [tableView dequeueReusableCellWithIdentifier:IDCell];
        if (cell == nil)
        {
            cell = [[PeopleOneCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:IDCell];
        }
     
        if ([cell isKindOfClass:[PeopleOneCell class]])
        {
            if (_peopleDic != nil)
            {
                [(PeopleOneCell *)cell setupPeopleCellWithHeaderImage:[_peopleDic objectForKey:@"img"] withNickName:[_peopleDic objectForKey:@"name"]];
            }
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        }
        return cell;
    }
    else
    {
        
        NSString *CellIdentifier1 = [NSString stringWithFormat:@"Cell%ld%ld", (long)[indexPath section], (long)[indexPath row]];
        PeopleTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        if (cell==nil)
        {
            
            cell = [[PeopleTwoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
        }

        cell.tuImg.image = [UIImage imageNamed:_imgArr[indexPath.row]];
      
        cell.titleLab.text = _titleArr[indexPath.row];
          [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
 
    }
  }

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        return 80;
    }
    else
    {
        return 50;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 100;
}


-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, 100)];
    view.backgroundColor = BgColor;
    [self.view addSubview:view];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(20, 30, B_WIDTH-20*2, 40);
    btn.layer.cornerRadius = 5;
    btn.backgroundColor = [UIColor orangeColor];
    [btn setTitle:@"退出" forState:0];
    [btn setTitleColor:[UIColor whiteColor] forState:0];
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    [btn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    return view;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
        if (indexPath.row == 0)
        {
            ZiViewController *ziVc = [[ZiViewController alloc] init];
            [self.navigationController pushViewController:ziVc animated:NO];
        }
        else if (indexPath.row  == 1)
        {
            MyLetterViewController *myLetterVc = [[MyLetterViewController alloc] init];
            [self.navigationController pushViewController:myLetterVc animated:NO];
        }
        else if (indexPath.row  == 2)
        {
            ChengJiViewController *chengJiVc = [[ChengJiViewController alloc] init];
            [self.navigationController pushViewController: chengJiVc animated:NO];
        }
//        else if (indexPath.row  == 3)
//        {
//            MyMoneyViewController *myMoneyVC = [[MyMoneyViewController alloc] init];
//            [self.navigationController pushViewController:myMoneyVC animated:NO];
//            
//            
//        }
        else
        {
            AboutViewController *aboutVC = [[AboutViewController alloc] init];
            [self.navigationController pushViewController:aboutVC animated:NO];
        }
}
    
-(void)BtnClick:(UIButton *)btn
{
    //切换到主线程执行
    [self performSelectorOnMainThread:@selector(showExitAlert) withObject:self waitUntilDone:NO];
    


    //清除缓存
    [self getCacheSize];
  
    [[SDImageCache sharedImageCache] clearDisk];
    [[NSFileManager defaultManager] removeItemAtPath:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES)lastObject]error:nil];
    
    

    
}

-(void)showExitAlert
{
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"确定要退出?" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"UID"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"USER_ID"];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"NetHeadImage"];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"LocalHeadImage"];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"STATE"];
         [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"PROJECT_NAME"];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"LOGIN_NAME"];
      [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"ONLY_URL"];
         [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"BTG"];
    
        
        ViewController *viewCC = [[ViewController alloc] init];
        [self presentViewController:viewCC animated:NO completion:nil];
       
        
    }]];
    
    [alert.view setTintColor:[UIColor colorWithRed:37/255.0 green:85/255.0 blue:100/255.0 alpha:1]];
    [self.navigationController presentViewController:alert animated:YES completion:nil];
}


//计算缓存量
- (double)getCacheSize
{
    SDImageCache *imageCache = [SDImageCache sharedImageCache];
    NSUInteger fileSize = [imageCache getSize]; // 以字节为单位
    NSString *myCachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES)lastObject];
    NSFileManager *fm = [NSFileManager defaultManager];
    NSDictionary *fileInfo = [fm attributesOfItemAtPath:myCachePath error:nil];
    fileSize += fileInfo.fileSize;
    return fileSize/1024.0/1024.0;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
